package com.bjsxt.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.bjsxt.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class TestController {

    @GetMapping("/test")
    public ResponseEntity<String> test(){
        return ResponseEntity.ok("test") ;
    }


    @GetMapping("/thread")
    public ResponseEntity<String> thread() throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        return ResponseEntity.ok("thread") ;
    }

    @GetMapping("/test1")
    public ResponseEntity<String> test1(){
        return ResponseEntity.ok("test1") ;
    }

    @GetMapping("/test2")
    public ResponseEntity<String> test2(){
        return ResponseEntity.ok("test2") ;
    }

    @Autowired
    private TestService testService ;


    @GetMapping("/link1")
    public ResponseEntity<String> link1(){
        String hello = testService.hello();
        System.out.println("调用hello资源成功");
        return ResponseEntity.ok("link1->hello()->other") ;
    }

    @GetMapping("/link2")
    public ResponseEntity<String> link2(){
        String hello = testService.hello();
        System.out.println("调用hello资源成功");
        return ResponseEntity.ok("link2->hello()->other") ;
    }

    private static Logger logger = LoggerFactory.getLogger(TestController.class) ;

    @GetMapping("/queue")
    public ResponseEntity<String> queue(){
        logger.info("开始处理了");
        return ResponseEntity.ok("ok") ;
    }

    /**
     * 1s 5 个请求
     * 1个请求处理的时间超过500ms
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/rt")
    public ResponseEntity<String> rt() throws InterruptedException {
       TimeUnit.SECONDS.sleep(1);

       return ResponseEntity.ok("rt") ;
    }

    @GetMapping("/exception")
    public ResponseEntity<String> exception(){
        throw new RuntimeException("就是不想成功") ;
    }

    @GetMapping("/buy")
    @SentinelResource("buy")
    public ResponseEntity<String> buy(String prodName ,Integer count){
        return ResponseEntity.ok(String.format("购买%s共%s",prodName,count)) ;
    }

  }
